﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Battleship
{
    public class LoadingSpace
    {
        public int numTier;
        public int numBay;
        public int numRow;
        public ContainerSlot[, ,] content;
        public LoadingSpace(int nT, int nB, int nR)
        {
            numTier = nT;
            numBay = nB;
            numRow = nR;
            content = new ContainerSlot[numTier, numBay, numRow];
            for (int i = 0; i < numTier; i++)
            {
                for (int j = 0; j < numBay; j++)
                {
                    for (int k = 0; k < numRow; k++)
                    {
                        content[i, j, k] = new ContainerSlot(i, j, k);
                    }
                }
            }
            setPri();
        }
        public LoadingSpace()
        {
            numTier = 5;
            numBay = 8;
            numRow = 4;
            content = new ContainerSlot[numTier, numBay, numRow];
            for (int i = 0; i < numTier; i++)
            {
                for (int j = 0; j < numBay; j++)
                {
                    for (int k = 0; k < numRow; k++)
                    {
                        content[i, j, k] = new ContainerSlot(i, j, k);
                    }
                }
            }
            setPri();
        }

        // set container priority in contructor
        public void setPri()
        {
            for (int i = 0; i < 5; i++)
            {
                // 1
                content[i, 3, 1].pType = 1;
                content[i, 3, 2].pType = 1;
                content[i, 4, 1].pType = 1;
                content[i, 4, 2].pType = 1;

                // 2
                for (int j = 2; j < 6; j++)
                {
                    content[i, j, 0].pType = 2;
                    content[i, j, 3].pType = 2;
                }
                content[i, 2, 1].pType = 2;
                content[i, 2, 2].pType = 2;
                content[i, 5, 1].pType = 2;
                content[i, 5, 2].pType = 2;

                // 3
                for (int k = 0; k < 4; k++)
                {
                    content[i, 1, k].pType = 3;
                    content[i, 6, k].pType = 3;
                }

                // 4
                for (int l = 0; l < 4; l++)
                {
                    content[i, 0, l].pType = 4;
                    content[i, 7, l].pType = 4;
                }
            }
        }

        // valuate loading space moment
        public Moment valMoment()
        {
            double sumX = 0;
            double sumY = 0;
            double sumZ = 0;
            foreach (ContainerSlot item in content)
            {
                item.valMoment();
                if (item.container != null)
                {
                    sumX += item.moment.X * item.container.Weight;
                    sumY += item.moment.Y * item.container.Weight;
                    sumZ += item.moment.Z * item.container.Weight;
                }
                else
                {
                    ;
                }
            }
            Moment moment = new Moment(sumX, sumY, sumZ);
            return moment;
        }

        // get list of ORD container on tierNum
        public List<ContainerSlot> getListOrd20OfTier(int tierNum)
        {
            Navigation root = new Navigation(tierNum, tierNum, 0, 7, 0, 3);
            List<ContainerSlot> result = new List<ContainerSlot>();
            do
            {
                int t = root.cursor.tier;
                int b = root.cursor.bay;
                int r = root.cursor.row;
                ContainerSlot tmp = this.content[t, b, r];
                if (tmp.container != null)
                {
                    if (tmp.container.Type == "ORD" && tmp.container.Size == 20)
                    {
                        result.Add(tmp);
                    }
                }
            } while (root.Next());
            return result;
        }

        public List<ContainerSlot> getAllSlot20OfTier(int tierNum)
        {
            Navigation root = new Navigation(tierNum, tierNum, 0, 7, 0, 3);
            List<ContainerSlot> result = new List<ContainerSlot>();
            do
            {
                int t = root.cursor.tier;
                int b = root.cursor.bay;
                int r = root.cursor.row;
                ContainerSlot tmp = this.content[t, b, r];
                if (tmp.container != null)
                {
                    if (tmp.container.Type == "ORD" && tmp.container.Size == 20)
                    {
                        result.Add(tmp);
                    }
                }
                else
                {
                    result.Add(tmp);
                }

            } while (root.Next());
            {

            }
            return result;
        }
        // check if swap operation ok or not
        public bool isSwapOK(ContainerSlot A, ContainerSlot B)
        {
            if (A.container != null && B.container != null)
            {
                if (A.container.Size == B.container.Size)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            else if (A.container == null && B.container != null)
            {
                if (B.container.Size == 40)
                {
                    int t = A.location.tier;
                    int b = A.location.bay;
                    int r = A.location.row;
                    if (b == 1 || b == 3 || b == 5 || b == 7)
                    {
                        return false;
                    }
                    if (content[t, b + 1, r].container == null)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return true;
                }
            }
            else if (B.container == null && A.container != null)
            {
                if (A.container.Size == 40)
                {
                    int t = B.location.tier;
                    int b = B.location.bay;
                    int r = B.location.row;
                    if (b == 1 || b == 3 || b == 5 || b == 7)
                    {
                        return false;
                    }
                    if (content[t, b + 1, r].container == null)
                    {
                        return true;
                    }
                    else
                        return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }

        }

        // swap between 2 slot
        //public void swap(ContainerSlot A, ContainerSlot B)
        //{
        //    if (isSwapOK(A, B))
        //    {

        //        {
        //            int tA = A.location.tier;
        //            int bA = A.location.bay;
        //            int rA = A.location.row;

        //            //B.container = A.container;
        //            if (B.container != null)
        //            {
        //                B.container.GCor = new Cor(tA, bA, rA);
        //                B.container.valMoment();
        //                A.container = null;
        //            }
        //        }

        //        {
        //            int tB = B.location.tier;
        //            int bB = B.location.bay;
        //            int rB = B.location.row;
        //            if (A.container != null)
        //            {
        //                A.container.GCor = new Cor(tB, bB, rB);
        //                A.container.valMoment();
        //                B.container = null;
        //            }
        //        }

        //        if (A.container != null && B.container != null)
        //        {
        //            Container tmp = A.container;
        //            A.container = B.container;
        //            B.container = tmp;
        //        }
        //    }
        //}
        public bool swap(SlotPoint locFrom, SlotPoint locTo)
        {
            int t1 = locFrom.tier;
            int b1 = locFrom.bay;
            int r1 = locFrom.row;

            int t2 = locTo.tier;
            int b2 = locTo.bay;
            int r2 = locTo.row;

            ContainerInfo tmp = this.content[t2, b2, r2].container;
            this.content[t2, b2, r2].container = this.content[t1, b1, r1].container;
            this.content[t1, b1, r1].container = tmp;
            return true;
        }
        
    }
}
